package com.guigu.health.log;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @ClassName:我的日记配置类
 * @Description:
 * @Author:longtao
 * @Date:2021/3/23
 * @Version:1.0
 */
@Component
@Aspect
public class MyDiaryConfigurationClass {
    private Logger logger=Logger.getLogger(MyDiaryConfigurationClass.class);
    /**
     * 前置增强
     *
     * @param joinPoint
     * execution(* *..service..*.*(..))
     */
    @After("execution(* *..service..*.*(..))")
    public void Before(JoinPoint joinPoint) { logger.info("前置增强:调用了" + joinPoint.getTarget() + "类的" + joinPoint.getSignature().getName() + "方法," +
                "参数为:" + Arrays.toString(joinPoint.getArgs()));
    }

    /**
     * 后置增强
     *
     * @param joinPoint
     * @param result
     */
    @AfterReturning(value = "execution(* *..service..*.*(..))", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {
        logger.info("后置增强:调用了" + joinPoint.getTarget() + "类的" + joinPoint.getSignature().getName() + "方法," +
                "参数为:" + Arrays.toString(joinPoint.getArgs()) + "返回值结果为:" + result);
    }

    /**
     * 异常抛出增强
     * @param joinPoint
     * @param e
     */
    @AfterThrowing(value = "execution(* *..service..*.*(..))", throwing = "e")
    public void afterThrowing(JoinPoint joinPoint, Exception e) {
        logger.info("异常抛出增强:" + joinPoint.getTarget() + "类的方法名:" + joinPoint.getSignature().getName() + "的参数:" + Arrays.toString(joinPoint.getArgs()) + "抛出的异常:" + e);

    }

    /**
     * 最终增强
     * @param joinPoint
     */
    @After("execution(* *..service..*.*(..))")
    public void After(JoinPoint joinPoint) {
        logger.info("最终增强:调用了" + joinPoint.getTarget() + "类的" + joinPoint.getSignature().getName() + "方法," +
                "参数为:" + Arrays.toString(joinPoint.getArgs()));
    }
}
